package givenchy
import org.codehaus.groovy.grails.plugins.springsecurity.Secured
@Secured(['ROLE_USER','ROLE_ADMIN'])
class TaskGroupController {
    def authenticateService
    
    def beforeInterceptor = [action:this.&auth,only:['create','save']]
    // defined as a regular method so its private
    def auth() {
         if((authenticateService.userDomain().vip < grailsApplication.config.grails.vip.large.toInteger()) && (TaskGroup.countByPerson(authenticateService.userDomain()) > 0)){
                flash.message = "您只能拥有一个任务组"
                return redirect(action: "list")
         }
    }

    
    static allowedMethods = [save: "POST", update: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        if(TaskGroup.countByPerson(authenticateService.userDomain()) == 0){
            flash.message = "每一个任务都将依附于一个组中，您还没有任务组，请先添加！"
            return redirect(action: "create")
        }
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [taskGroupInstanceList: TaskGroup.findAllByPerson(authenticateService.userDomain(),params), taskGroupInstanceTotal: TaskGroup.countByPerson(authenticateService.userDomain())]
    }

    def create = {
        def taskGroupInstance = new TaskGroup()
        taskGroupInstance.properties = params
        return [taskGroupInstance: taskGroupInstance]
    }

    def save = {
        def taskGroupInstance = new TaskGroup(params)
        if(authenticateService.userDomain().vip < grailsApplication.config.grails.vip.great.toInteger()){
            taskGroupInstance.isAd = false
            taskGroupInstance.requireOfIP = 0
        }
        taskGroupInstance.person = authenticateService.userDomain()
        taskGroupInstance.buildHourRate(params.rate)
        if (taskGroupInstance.save(flush: true)) {
            flash.message = "任务组创建成功"
            redirect(action: "list")
        }
        else {
            render(view: "create", model: [taskGroupInstance: taskGroupInstance])
        }
    }

    def edit = {
        def taskGroupInstance = TaskGroup.get(params.id)
        if (!taskGroupInstance) {
            flash.message = "不存在此任务组"
            redirect(action: "list")
        }
        else {
            return [taskGroupInstance: taskGroupInstance]
        }
    }

    def update = {
        def taskGroupInstance = TaskGroup.get(params.id)
        if (taskGroupInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (taskGroupInstance.version > version) {
                    
                    taskGroupInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'taskGroup.label', default: 'TaskGroup')] as Object[], "Another user has updated this TaskGroup while you were editing")
                    render(view: "edit", model: [taskGroupInstance: taskGroupInstance])
                    return
                }
            }
            taskGroupInstance.properties = params
            taskGroupInstance.buildHourRate(params.rate)
            if (!taskGroupInstance.hasErrors() && taskGroupInstance.save(flush: true)) {
                flash.message = "更新成功"
                redirect(action: "list")
            }
            else {
                render(view: "edit", model: [taskGroupInstance: taskGroupInstance])
            }
        }
        else {
            flash.message = "不存在此任务组"
            redirect(action: "list")
        }
    }

    def delete = {
        def taskGroupInstance = TaskGroup.get(params.id)
        if (taskGroupInstance) {
            try {
                taskGroupInstance.tasks.each{
                    taskGroupInstance.removeFromTasks(it)
                    it.delete(flush: true)               
                }
                taskGroupInstance.delete(flush: true)
                flash.message = "删除成功！"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "删除失败！"
                redirect(action: "list")
                return 
            }
        }
        else {
            flash.message = "任务组不存在"
            redirect(action: "list")
            return
        }
    }
}
